<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	<link rel="stylesheet" type="text/css" href="<%=basePath %>admin/js/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>admin/test/css/icon.css">
	<script type="text/javascript" src="<%=basePath %>admin/test/js/jquery-1.3.2.min.js"></script>
	<script type="text/javascript" src="<%=basePath %>admin/js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<%=basePath %>admin/js/easyui-lang-zh_CN.js"></script>
	
		<link rel="stylesheet" type="text/css" href="<%=basePath %>admin/js/jquery-1.4.4.min.js">
		<script type="text/javascript" src="<%=basePath %>admin/js/jquery.datepick.js"></script>  
    <link type="text/css" href="<%=basePath %>admin/test/css/flora.datepick.css" rel="stylesheet">
<script type="text/javascript" src="<%=basePath %>admin/js/jquery.datepick-zh-CN.js"></script>  
	
	<script>
		$(function(){
			$('#test').datagrid({
				title:'增加科室',
				 iconCls:'icon-ok',
				width:1080,
				height:550,
				pageNumber:1,
				pageSize:10,
				 pageList:[5,10,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,45,50,80,100],
                collapsible:true,
				nowrap: true,
				//fit:true,自动适应屏幕宽度
				striped: true,
				url:'<%=basePath%>initKeshikeshiAction',
				sortName: 'id',
				sortOrder:'desc',
				rownumbers:true,
				idField:'id',
				queryParam:{},
				singleSelect:false,
				frozenColumns:[[
                 {field:'ck',checkbox:true}
              ]],
	        columns:[[           
            {field:'id',title:'编号',width:60,sortable:true,editor:'text'},
			{field:'name',title:'科室名称',width:120,sortable:true,editor:'text'},
			{field:'hid',title:'所属医院',width:120,sortable:true,editor:'text'},
			{field:'duty',title:'职责',width:180,sortable:true,editor:'text'},
			{field:'inteoduce',title:'科室简介',width:420,editor:'text'},
			{field:'operator',title:'操作列',width:70,align:'center',
          formatter:function(value,row,index){
        if(row.editing)
        {
            var s ='<a href="javascript:void(0);" onclick="saveRow('+index+')" style="text-decoration: none;color: #800080;">保存</a>  ';
            var c ='<a href="javascript:void(0);" onclick="cancelRow('+index+')" style="text-decoration: none;color: #800080;">取消</a>';
            return s+c;
        }
        else
        {
            var e = '<img src="<%=basePath%>admin/test/css/icons/btn_edit.png" onclick="editRow('+index+')" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
          var d = '<img src="<%=basePath%>admin/test/css/icons/btn_delete.png" onclick="delRow('+index+')" > ';
        return e+d;
        }
    }
}
				]],
				pagination:true,
				rownumbers:true,
				toolbar:[{
             text:'增加',
             iconCls:'icon-add',
             handler:function()
             {
                   $('#addKeshi').show();
			$('#addKeshi').dialog({
			buttons:[{
						text:'增加',
						iconCls:'icon-save',
						handler:function()
						{
							addKeshi();
						}
					},{
						text:'取消',
						handler:function()
						{
							$('#add').dialog('close');
							//$("#test").datagrid("loaded");
						}
					}]
			});
             }
          },'-',
          {
  			  text:'查询',
              iconCls:'icon-search',
              handler:function()
              {
                  $('#query').window('open');
              }
          },'-',
          {
                    id:'btncut',
					text:'批量删除',
					iconCls:'icon-cut',
					handler:function()
					{
						delUserSelect();
					}
          }
        ],
		onSortColumn:function(sort,order)
		{
			alert(sort+":"+order)
		}
				
			});
		});
		
		/*删除提示*/
		function delUserSelect(){
			var rows = $('#test').datagrid('getSelections');
			if(rows.length>0)
			{
				del();
			}
			else
			{
			   $.messager.alert('提醒','请选择一行','info');
				return false;
			}
		}
		//确认删除
		function  del()
		{
			$.messager.confirm('确认','你确定要删除吗?',function(r)
			{
			   if (r)
			   {
			    	myAjaxDel();
			   }	
	   		});
		}
		//删除操作
		function myAjaxDel()
		{
			var ids = [];
			var rows = $('#test').datagrid('getSelections');
			for(var i=0;i<rows.length;i++)
			{
				ids.push(rows[i].id);
			}
			$.ajax({
				   type: 'post',
				   url: "delKeshikeshiAction",
				   data: "delKeshiIds="+ids,
				   success: function(data)
				   {
				     if(data=='ok')
				     {
						$.messager.alert('提醒','删除成功','info');
						var rows2 = $('#test').datagrid('getSelections');//获取你选择的所有行
						//循环所选的行
						for(var i =0;i<rows2.length;i++)
						{
							var index2 = $('#test').datagrid('getRowIndex',rows2[i]);//获取某行的行号
							$('#test').datagrid('deleteRow',index2);	//通过行号移除该行
						}
						
					}
					if(data=='error')
					{
						$.messager.alert('提醒','删除失败','info');
					}
				  }
			});
      }
		
		/*获取选中的编号*/
		function getSelected()
		{
			var selected = $('#test').datagrid('getSelected');
			alert(selected.id+":"+selected.name);
			
		}
		/*获取所有选中的编号*/
		function getSelections()
		{
			var ids = [];
			var rows = $('#test').datagrid('getSelections');
			for(var i=0;i<rows.length;i++)
			{
				ids.push(rows[i].id);
			}
			
		}
		function myformatter(value,rec)
		{
			return 'a:'+value+'>'+rec.name;
		}
      //提交新新增
      //提交保存
	function addKeshi()
	{
		$('#addKeshiForm').form({
			    	success:function(data)
			    	{
			    	
			    		if(data=='ok'){
			    			$.messager.alert('后台提示', '数据更新成功', 'info');
			    			//初始化
			    			$('#addKeshi').dialog('close');
			    			$('#test').datagrid('reload');
			    		}
						
						if(data=='error')
						{
						    $('#addKeshi').dialog('close');
			    			$.messager.alert('后台提示', '数据更新失败', 'info');
			    		}
			    	}	
				}).submit();
	}	
    //提交更新
	function updateKeshi()
	{
		$('#UpdateKeshiForm').form({
			    	success:function(data)
			    	{
			    	
			    		if(data=='ok'){
			    			$.messager.alert('后台提示', '数据更新成功', 'info');
			    			//初始化
			    			$('#dd').dialog('close');
			    			$('#test').datagrid('reload');
			    		}
						
						if(data=='error')
						{
							$('#dd').dialog('close');
			    			$.messager.alert('后台提示', '数据更新失败', 'info');
			    		}
			    	}	
				}).submit();
	}	
    /*单个删除操作*/
    function delRow(index)
    {
           var selected = $('#test').datagrid('getSelected');//获取你选择的所有行
          
           if(selected!=null)
           {
	             $.messager.confirm('确认','你确定要删除吗?',function(r)
				  {
					  if(r)
					  {
					      $.ajax({
								   type: 'post',
								   url: "delKeshikeshiAction",
								   data: "delKeshiIds="+selected.id,
								   success: function(data)
								   {
								     if(data=='ok')
								     {
										$.messager.alert('提醒','删除成功','info');
										//删除所选的行
										var index2 = $('#test').datagrid('getRowIndex',selected);//获取某行的行号
										$('#test').datagrid('deleteRow',index2);	//通过行号移除该行
									}
									if(data=='error')
									{
										$.messager.alert('提醒','删除失败','info');
									}
								  }
							});
					  }
				  })
			}
			else
			{
			  $.messager.alert('后台提示', '请在要删除的行前打钩', 'info');
			  return false;
			}			
		 
    }
		/*编辑医院信息*/
		function editRow(index)
		{
           // $('#test').datagrid('beginEdit',index);
      
        var rows = $('#test').datagrid('getSelections')
		 if(rows.length==1)
		 {
			editing();			
		 }
		 else
		 {
			$.messager.alert('提醒','请选择一行','info');
			return false;
		 }
        }
function editing()
     {
    	var selected = $('#test').datagrid('getSelected');
		if (selected)
		{
			//给页面赋值
			$('#id').val(selected.id);
			$('#name').val(selected.name);
			$('#hid').val(selected.hid);
			$('#duty').val(selected.duty);
			$('#inteoduce').val(selected.inteoduce);
			
			$('#dd').show();
			$('#dd').dialog({
			buttons:[{
						text:'更新',
						iconCls:'icon-save',
						handler:function()
						{
							updateKeshi();
						}
					},{
						text:'取消',
						handler:function()
						{
							$('#dd').dialog('close');
							//$("#test").datagrid("loaded");
						}
					}]
			});
					
		}
    }
    /*保存医院信息*/
     function saveRow(index)
     {
      
     $('#test').datagrid('endEdit',index);
        /*
         * 将数据保存到数据库
         * */
        var select = $('#test').datagrid('getSelected');
        var arr =new Array(); //将信息保存在数组中
        arr[0]= select.id;
        arr[1]= select.name;
        arr[2]= select.leve;
        arr[3]= select.address;
        arr[4]= select.telphone;
        arr[5]=select.weburl;
        if(select){
            $.ajax({
            type:'POST',
            url:'<%=basePath%>addHospiatlhospitalAction',
            data:'id='+select.id,
            success:function(data){
                if(data=='0'){
                    $.messager.alert('error','学号'+select.id+'已存在,请重新编辑','error',function(){
                        $('#tt').datagrid('beginEdit', index);
                    });
                }else{
                    save(arr,index);
                }
            }
        });
        }
        
       
    }
   

        /*保存医院*/
        function save(arr,index)
        {
             $.ajax({
            type:'POST',
            url:'saveInfo.action',
            data:'arr='+arr,
            success:function(data){
                if(data){
                    $.messager.alert('warning',data,'warning',function(){
                        $('#tt').datagrid('beginEdit', index);
                    })
                }else{
                    $('#tt').datagrid('reload');
                }
            }
        });
    }
   
    /*查询*/
     function query()
     {
  
        var queryParams = $('#test').datagrid('options').queryParams;
        queryParams.queryType = $('#queryType').val();
        queryParams.queryValue = $('#queryValue').val();
        	$('#test').datagrid(
        	{
        	     
            	url:'<%=basePath%>queryKeshikeshiAction'
        	});
        	$('#query').window('close');        
        
    }
    
        
  
	</script>
</head>
<body>
	<table id="test"></table>
	
	 <div id="dd" icon="icon-edit" style="display:none;padding-top:30px;width:750px;height:450px;">
  <div style="width:700px;height:auto;background:#fcfcfc;" >
  <form action="<%=basePath %>updateKeshikeshiAction" id="UpdateKeshiForm"  method="post">
		<table class="grid" id="uiform" >
			<tr>
				<td>
					编号：
				</td>
				<td>
					<input  id="id"  name="keshis.id" type="text" class="txt03" value="" readonly="readonly"/>
				</td>
				<td>
					*科室名称：
				</td>
				<td>
					<input id="name"  type="text" name="keshis.name"   class="txt03" value="" />
				</td>
			</tr>
			<tr>
				<td>
					*职责：
				</td>
				<td>
					<input id="duty"   maxlength="20" name="keshis.duty" class="txt03"  value=""  />
				</td>
					<td>
					*科室简介：
				</td>
				<td>
					<input id="inteoduce"   name="keshis.inteoduce"  class="txt03"  value=""  />
				</td>
			</tr>
			<tr>
				<td>
					*所属医院：
				</td>
				<td>
					<input id="hid"   maxlength="20" name="keshis.hospital.name" class="txt03"  value=""  />
				</td>
					
			</tr>
				
		</table>
		</form>
</div></div>
	
	<!-- 新增科室 -->
	<div id="addKeshi" icon="icon-edit" title="增加医院信息" style="display:none;padding-top:30px;width:750px;height:450px;">
  <div style="width:700px;height:auto;background:#fcfcfc;" >
  <form action="<%=basePath %>addKeshikeshiAction" id="addKeshiForm"  method="post">
		<table class="grid" id="uiform" >
			<tr>
				<td>
					*科室名称：
				</td>
				<td>
					<input id="name"  type="text" name="keshis.name"   class="txt03" value="" />
				</td>
				<td>
					*所属医院：
				</td>
				<td>
					<select style="width:150px;" name="keshis.hospital.name">
					<c:forEach  items="${hs}" var="hs">
					<option value="${hs.name}">${hs.name }</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					*科室职责：
				</td>
				<td>
					<input id="duty"   maxlength="20" name="keshis.duty" class="txt03"  value=""  />
				</td>
					<td>
					*科室简介：
				</td>
				<td>
					<input id="inteoduce"   name="keshis.inteoduce"  class="txt03"  value=""  />
				</td>
			</tr>
				
				
		</table>
		</form>
</div></div>
	<!-- 查询科室 -->
	<div id="query" class="easyui-window" title="查询" style="padding: 10px;width: 400px;height:120;"
    iconCls="icon-search" closed="true" maximizable="false" minimizable="false" collapsible="false">
        <div>
            <table>
                <tr>
                    <td>
                        <select name="select" id="queryType">
                            <option value="id">编号</option>
                            <option value="name">科室名称</option>
                         
                        </select>
                    </td>
                    <td><input type="text" style="background:#fcfcfc;" name="queryValue" id="queryValue"  value="" /></td>
                    <td><a class="easyui-linkbutton" iconCls="icon-search" href="javascript:void(0);" onclick="query()">查询</a></td>
                </tr>
            </table>
        </div>
    </div>
</body>
</html>